import { PhysicsViewer } from "@babylonjs/core/Debug/physicsViewer";
import {
    Engine,
    Scene,
    ArcRotateCamera,
    Vector3,
    Quaternion,
    HemisphericLight,
    MeshBuilder,
    HavokPlugin,
    PhysicsAggregate,
    PhysicsShapeType,
    HingeConstraint,
    PhysicsShapeContainer,
    PhysicsShapeBox,
    PhysicsShapeSphere,
    PhysicsBody,
    PhysicsMotionType
} from "@babylonjs/core";
import { createGround } from "./createGround";

async function createSinglePendulum(scene: Scene, id: string | number) {
    const parentName = `ParentBall-${id}`;
    const parentBall = MeshBuilder.CreateSphere(parentName, { diameter: 1 }, scene);

    const ballName = `PendulumBall-${id}`;
    const ball = MeshBuilder.CreateSphere(ballName, { diameter: 1 }, scene);
    ball.isVisible = false;

    const pivotName = `PendulumPivot-${id}`;
    const pivot = MeshBuilder.CreateBox(
        pivotName,
        {
            width: 0.1,
            height: 5,
            depth: 0.1
        },
        scene
    );
    pivot.position.y += 2.5;
    pivot.isVisible = false;

    const ballShape = new PhysicsShapeSphere(new Vector3(0, 0, 0), 0.5, scene);
    const pivotShape = new PhysicsShapeBox(
        new Vector3(0, 0, 0),
        new Quaternion(0, 0, 0, 1),
        new Vector3(0.1, 5, 0.1),
        scene
    );

    const pendulumShape = new PhysicsShapeContainer(scene);
    pendulumShape.addChildFromParent(parentBall, ballShape, ball);
    pendulumShape.addChildFromParent(parentBall, pivotShape, pivot);

    const body = new PhysicsBody(parentBall, PhysicsMotionType.DYNAMIC, false, scene);
    body.shape = pendulumShape;
    body.setMassProperties({
        mass: 1
    });
    new PhysicsViewer().showBody(body);
}

export async function testPhysicsShapeContainer(scene: Scene) {
    createGround(scene);
    createSinglePendulum(scene, 1);
}
